{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h1>FXCM REST API Demonstration</h1>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "<h3>Connecting to FXCM REST API</h3>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#import fxcmpy and check the imported version\n",
    "import fxcmpy\n",
    "import datetime as dt\n",
    "fxcmpy.__version__\n",
    "\n",
    "token = 'YOUR_TOKEN_HERE'\n",
    "\n",
    "#Use the config file to connect to the API. \n",
    "con = fxcmpy.fxcmpy(access_token = token, log_level='error',log_file=None)\n",
    "\n",
    "#The server is demo by default. THe options below are also available for usage.\n",
    "#con = fxcmpy.fxcmpy(config_file='fxcm.cfg', server='demo')\n",
    "\n",
    "#Connect to the API with a real account. Do not forget to change the access token in the config file.\n",
    "#con = fxcmpy.fxcmpy(config_file='fxcm.cfg', server='real')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "<h3>Getting Instruments</h3>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "# All Tradable Instruments\n",
    "\n",
    "con.get_instruments()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "# Instruments that your account is subscribed to\n",
    "\n",
    "con.get_instruments_for_candles()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h3>Pulling Historical Prices</h3>\n",
    "\n",
    "Available periods : 'm1', 'm5', 'm15', 'm30', 'H1', 'H2', 'H3', 'H4', 'H6', 'H8','D1', 'W1', or 'M1'."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#Getting historical prices by specifying only the period.  \n",
    "\n",
    "con.get_candles('EUR/JPY', period='m1')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "#Getting historical prices by specifying the number of results you would like to see.\n",
    "\n",
    "con.get_candles('EUR/USD', period='m1', number=100)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "#Getting historical Prices by Specifying the start and the end date.\n",
    "\n",
    "start = dt.datetime(2018, 1, 14)\n",
    "stop = dt.datetime(2018, 5, 1)\n",
    "\n",
    "con.get_candles('EUR/USD', period='D1',\n",
    "                start=start, stop=stop)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "<h3>Streaming Real-time Prices</h3>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Subscribe To Market Data\n",
    "\n",
    "con.subscribe_market_data('EUR/USD')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Once you are subscribed the fxcm.py collects the data in a pandas DataFrame. \n",
    "\n",
    "con.get_prices('EUR/USD')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#To stop the stream and delete the dataframe\n",
    "\n",
    "con.unsubscribe_market_data('EUR/USD')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "<h3>Executing Orders</h3>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Check whether you have an open positions already.\n",
    "\n",
    "con.get_open_positions()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Create a market order\n",
    "\n",
    "order = con.create_market_buy_order('USD/JPY', 10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#Checking The New Oen Position Table\n",
    "\n",
    "con.get_open_positions().T"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Place an order with more control over parameters\n",
    "\n",
    "order2 = con.open_trade(symbol='EUR/JPY', is_buy=True,\n",
    "                       rate=105, is_in_pips=False,\n",
    "                       amount='10', time_in_force='GTC',\n",
    "                       order_type='AtMarket', limit=150)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "<h3>Closing Orders</h3>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Close position by specifying the tradeId\n",
    "\n",
    "pos = con.get_open_position(65680347)\n",
    "pos.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Close positions by specifying the currency pair\n",
    "\n",
    "con.close_all_for_symbol('USD/JPY')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# CLose all the positions at once\n",
    "\n",
    "con.close_all()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
